package com.yx.changdao.web.controller;

import com.yx.changdao.common.annotation.AutoLog;
import com.yx.changdao.common.annotation.ShiroDesc;
import com.yx.changdao.common.data.Result;
import com.yx.changdao.common.entity.SysPermission;
import com.yx.changdao.common.exception.AlertException;
import com.yx.changdao.common.utils.SysUtils;
import com.yx.changdao.service.SysPermissionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @Description: <br />
 * 权限接口
 * <p>
 * <br />
 * @Auther: Zhangjiashun
 * @create 2019/11/18 13:22
 */
@RestController
@RequestMapping(value = "/sys/permission", produces = {"application/json;charset=UTF-8"})
public class SysPermissionController {

    private final SysPermissionService sysPermissionService;

    public SysPermissionController(SysPermissionService sysPermissionService) {
        this.sysPermissionService = sysPermissionService;
    }

    /**
     * 权限列表
     *
     * @return Result
     */
    @AutoLog
    @GetMapping("/list")
    @RequiresPermissions("sys:permission:list")
    @ShiroDesc("系统权限:列表")
    public Result list() throws AlertException {
        Map<String, List<SysPermission>> permGrpByPerfixMap =
                this.sysPermissionService.permissionToMap(SysUtils.autoCnd(SysPermission.class));
        return Result.ok().data(permGrpByPerfixMap);
    }


    /**
     * 获得指定id 权限信息。
     *
     * @param permissionId 权限id
     * @return Result
     */
    @AutoLog
    @GetMapping("/{id}")
    @RequiresPermissions("sys:permission:look")
    @ShiroDesc("系统权限:查看")
    public Result get(@PathVariable("id") String permissionId) throws AlertException {
        SysPermission permission = this.sysPermissionService.getPermission(permissionId);
        if (permission == null) {
            throw new AlertException("未找到相关权限信息");
        }
        return Result.ok().data(permission);
    }


}
